using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;

namespace Org.JSON
{
	public class Object : Value, IEnumerable<KeyValuePair<string,Value>>
	{
		private Dictionary<string,Value> value = new Dictionary<string, Value>();

		public Object Add(string property, Value value)
		{
			this.SafeAddCheck(value);

			this.value.Add(property, value);

			return this;
		}

		public bool Has(string property)
		{
			return this.value.ContainsKey(property);
		}

		public override void Serialize (System.IO.TextWriter stream)
		{
			stream.Write('{');

			var first = true;

			foreach(var item in this)
			{
				if(first)
				{
					first = false;
				}
				else
				{
					stream.Write(',');
				}
				new String(item.Key).Serialize(stream);
				stream.Write(':');
				item.Value.Serialize(stream);
			}

			stream.Write('}');
		}

	 	internal override bool DoesRecursion (Value value)
		{
			return base.DoesRecursion (value) || this.value.Values.Any( x=>x.DoesRecursion(value));
		}

		public IEnumerator<KeyValuePair<string,Value>> GetEnumerator()
	    {
	        return this.value.GetEnumerator();
	    }

		IEnumerator IEnumerable.GetEnumerator()
	    {
	        return this.GetEnumerator();
	    }

	}
}

